#include "stdafx.h"

BOOL Initialize ();	// Performs All Your Initialization

void Deinitialize (void);							// Performs All Your DeInitialization

void Update (DWORD milliseconds);					// Perform Motion Updates

void Draw (void);									// Perform All Your Scene Drawing

void Selection (void);								// Perform Selection



// GLUT functions
void renderScene();
void changeSize(int w, int h);
void processNormalKeys(unsigned char key, int x, int y);
void releaseKey(unsigned char key, int x, int y);

int mouse_x, mouse_y;
int window_x, window_y;
DWORD				tickCount;										// Used For The Tick Counter

int gunstate = 0;
float gunangle = 0;

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);
	glEnable(GL_NORMALIZE);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_COLOR_MATERIAL);
}

void update(int value)
{
	if( gunstate > 0 )
	{
		if( gunstate <= 2 )
		{
			gunangle = 45.0 / 2.0 * (double)gunstate;
		}
		else
		{	// start 3 , end 23
			int s = 3;
			int e = 23;
			gunangle = -45.0/double( (e-s)*(e-s) ) * ((double)gunstate - (double)s)*((double)gunstate - (double)s) + 45.0; //45.0 / 20.0 * (double)( (23 - gunstate) );
		}

		gunstate++;

		if( gunstate > 23 )
		{
			gunangle = 0.0;
			gunstate = 0;
		}
	}

	glutPostRedisplay();
	glutTimerFunc(50, update, 0);
}

void processMouseClick(int button, int state, int x, int y)
{
	mouse_x = x;
	mouse_y = y;

	if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && gunstate == 0 )
	{
		gunstate = 1;
		Selection();
	}
}

void processMouseMove(int x, int y)
{
	mouse_x = x;
	mouse_y = y;
}

int main (int argc, char * argv[]) 
{
	srand(0);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(640, 480);
	glutCreateWindow("Killer Turtle");


	Initialize () ;

	

	//initRendering();
	glutDisplayFunc(renderScene);
	glutTimerFunc(0, update, 0);
	glutReshapeFunc(changeSize);
	glutKeyboardFunc(processNormalKeys);
	glutMouseFunc(processMouseClick);
	glutPassiveMotionFunc(processMouseMove);
	//glEnable(GL_DEPTH_TEST);
	//glewInit();

	glutSetCursor(GLUT_CURSOR_NONE);

	glutMainLoop();
	return 0;
}

void renderScene()
{
	//tickCount = GetTickCount ();				// Get The Tick Count
	Update (50);	// Update The Counter
	//window.lastTickCount = tickCount;			// Set Last Count To Current Count
	Draw ();									// Draw Our Scene

	glutSwapBuffers();
}

void changeSize(int w, int h)
{
	window_x = w;
	window_y = h;

	if (h == 0) // Prevent divide by zero
		h = 1;

	float ratio = 1.0 * w / h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, w, h);

	gluPerspective(45, ratio, 1, 1000);
	glMatrixMode(GL_MODELVIEW);

	glLoadIdentity();
	//gluLookAt(0.0, 20.0, 20.0,		-1.0, -1.0, -1.0,		0.0, 1.0, 0.0);

}

void processNormalKeys(unsigned char key, int x, int y)
{
	if (key == 27)
		exit(0);

	if (key == 't')	{
	}
}